<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>登陆</title>
    <link rel="stylesheet" href="../layui/css/layui.css">
    <link rel="stylesheet" href="../css/login.css">
    <style>
        #right{
            background-color: #fff;
            position: absolute;
            right: 150px;
            top: 150px;
            padding: 30px;
            border-radius: 5px;
        }
        #nameError,#pwdError,#typeError{
            display: inline-block;
            height: 15px;
            font-size: 14px;
        }
        .demo-login-container{width: 320px; margin: 21px auto 0;}
        .demo-login-other .layui-icon{position: relative; display: inline-block; margin: 0 2px; top: 2px; font-size: 26px;}
    </style>
</head>
<body>

<div id="right">
    <form class="layui-form" id="loginForm">
        <div class="demo-login-container" id="layui_center">
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-username"></i>
                    </div>
                    <input type="text" name="adminInfoName" value="小黄" id="LAY-user-login-account" lay-verify="required" placeholder="用户名" lay-reqtext="请填写用户名" autocomplete="off" class="layui-input input-val1" lay-affix="clear">
                    <label id="nameError" style="color: red"> </label>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-password"></i>
                    </div>
                    <input type="password" name="adminInfoPwd" value="xx0325" id="LAY-user-login-role_pwd" lay-verify="required" placeholder="密   码" lay-reqtext="请填写密码" autocomplete="off" class="layui-input input-val2" lay-affix="eye">
                    <label id="pwdError" style="color: red"></label>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-xs7">
                        <div class="layui-input-wrap">
                            <div class="layui-input-prefix">
                                <i class="layui-icon layui-icon-vercode"></i>
                            </div>
                            <input type="text" name="captcha" lay-verify="required||canvas" placeholder="验证码" lay-reqtext="请填写验证码" autocomplete="off" class="layui-input input-val3" lay-affix="clear">
                        </div>
                    </div>
                    <div class="layui-col-xs5">
                        <div style="margin-left: 10px;">
                            <!--                            <img src="https://www.oschina.net/action/user/captcha" onclick="this.src='https://www.oschina.net/action/user/captcha?t='+ new Date().getTime();" class="img">-->
                            <canvas id="canvas" width="123" height="38" style="background-color: #ccc"></canvas>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <input type="checkbox" name="remember" lay-skin="primary" title="记住密码">
            </div>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid btn" lay-submit lay-filter="demo-login">登录</button>
            </div>
        </div>
    </form>
</div>

<script src="../layui/layui.js"></script>
<script>
    layui.use(['element', 'jquery', 'layer',], function () {
        let element = layui.element;
        let layer = layui.layer;
        let $ = layui.jquery;

        function login(){
            let params = $("#loginForm").serialize();
            params = decodeURIComponent(params,true);
            $.ajax({
                url: '../adminInfo/login',
                type: 'post',
                data: params,
                success: function ( res ) {
                    // 登录成功
                    if (res.code === 200) {
                        let name = res.data.adminInfoName;
                        sessionStorage.setItem("adminInfoName",name)
                        layer.msg('登陆成功',{icon:6});
                        // 3.页面跳转
                        location.href = "first.html";
                        return ;
                    }else if(res.code == 10001){
                        let errors = JSON.parse(res.message);
                        $.each(errors,function (k,v){
                            if (k == 'adminInfoName'){
                                $("#nameError").text(v)
                            }
                            if (k == 'adminInfoPwd'){
                                $("#pwdError").text(v)
                            }
                        })
                    }else {
                        // 登录失败，弹出错误消息
                        layer.msg(res.message,{icon:5});
                    }
                }
            });
        }

        $(function () {
            var show_num = [];
            draw(show_num);

            $("#canvas").on('click', function () {
                draw(show_num);
            })
            $(".btn").on('click', function () {
                var val1 = $(".input-val1").val().toLowerCase();
                var val2 = $(".input-val2").val().toLowerCase();
                var val3 = $(".input-val3").val().toLowerCase();


                var num = show_num.join("");
                if (val1 === '') {
                    layer.msg('请填写用户名！',{icon: 5});
                    return false;
                }
                if (val2 === '') {
                    layer.msg('请输入密码！',{icon: 5});
                    return false;
                }
                if (val3 === '') {
                    layer.msg('请输入验证码！',{icon: 5});
                    return false;
                } else if (val3 === num) {
                    login();
                    $(".input-val").val("");
                    return false;
                } else {
                    layer.msg('验证码错误！请重新输入',{icon:5});
                    $(".input-val").val("");
                    $(".input-val3").val("");
                    draw(show_num);
                    return false;
                }
            })
        })
        function draw(show_num) {
            var canvas_width = $('#canvas').width();
            var canvas_height = $('#canvas').height();
            var canvas = document.getElementById("canvas"); //获取到canvas的对象，演员
            var context = canvas.getContext("2d"); //获取到canvas画图的环境，演员表演的舞台
            canvas.width = canvas_width;
            canvas.height = canvas_height;
            var sCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0";
            var aCode = sCode.split(",");
            var aLength = aCode.length; //获取到数组的长度

            for (var i = 0; i <= 3; i++) {
                var j = Math.floor(Math.random() * aLength); //获取到随机的索引值
                var deg = Math.random() * 30 * Math.PI / 180; //产生0~30之间的随机弧度
                var txt = aCode[j]; //得到随机的一个内容
                show_num[i] = txt.toLowerCase();
                var x = 10 + i * 20; //文字在canvas上的x坐标
                var y = 20 + Math.random() * 8; //文字在canvas上的y坐标
                context.font = "bold 23px 微软雅黑";

                context.translate(x, y);
                context.rotate(deg);

                context.fillStyle = randomColor();
                context.fillText(txt, 0, 0);

                context.rotate(-deg);
                context.translate(-x, -y);
            }
            for (var i = 0; i <= 5; i++) { //验证码上显示线条
                context.strokeStyle = randomColor();
                context.beginPath();
                context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
                context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
                context.stroke();
            }
            for (var i = 0; i <= 30; i++) { //验证码上显示小点
                context.strokeStyle = randomColor();
                context.beginPath();
                var x = Math.random() * canvas_width;
                var y = Math.random() * canvas_height;
                context.moveTo(x, y);
                context.lineTo(x + 1, y + 1);
                context.stroke();
            }
        }

        function randomColor() { //得到随机的颜色值
            var r = Math.floor(Math.random() * 256);
            var g = Math.floor(Math.random() * 256);
            var b = Math.floor(Math.random() * 256);
            return "rgb(" + r + "," + g + "," + b + ")";
        }
    });
</script>

</body>
</html>